package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.Option;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/7/20.
 */

public class EquipOptionAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<Option> list = new ArrayList<>();

    public EquipOptionAdapter(Context context, List<Option> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.option_view_item, parent, false);
        ;
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Holder holder1 = (Holder) holder;
        final Option option = list.get(position);
        holder1.tvName.setText(option.getPlantNo());
        holder1.lMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (openListener != null) {
                    openListener.toggle(option.getKey(), !option.isSelect(), option);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private OnOpenListener openListener;

    public interface OnOpenListener {
        void toggle(int key, boolean select, Option option);
    }

    public void setSelect(OnOpenListener openListener) {
        this.openListener = openListener;
    }

    class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.tvName)
        TextView tvName;
        @Bind(R.id.lMain)
        LinearLayout lMain;;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
